<%
layout("/layouts/platform.html"){
%>
    <header class="header navbar bg-white shadow">
		<div class="btn-group tool-button" style="padding:8px 0">
			<table>
           		<tbody>
           			<tr>
                  		<td>
                       		<input type="text" id="coupon_name" name="coupon_name" onclick="this.value=''" class="form-control" placeholder="卡券名" autofocus>
                        </td>
                        <td style="padding-left: 5px;">
                            <button id="searchBtn" type="button" class="btn btn-default"><i class="fa fa-search"></i>&nbsp;查询</button>
                      	</td>
                        </tr>
                    </tbody>
            </table>
		</div>
	    <div class="pull-right offscreen-right">
	         <a class="btn btn-default navbar-btn" href="${base}/platform/ud/coupon/add" ><i class="fa fa-plus"></i>&nbsp;新增</a>
		</div>
	</header>
    <div class="panel-body" style="padding-top: 70px;">
		<div class="box">
			<div class="box-body">
				<div class="col-sm-12">
					<div class="row">
				        <table class="table table-bordered table-hover mg-t datatable">
			                <thead>
			                <tr>
			                    <th>卡券名称</th>
			                    <th>使用提醒</th>
			                    <th>额度</th>
			                    <th>状态</th>
			                    <th>已发放数量</th>
			                    <th>操作</th>
        	        	</table>
	    			</div>
				</div>
			</div>
		</div>
	</div>
<!-- 删除卡券 -->
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">删除卡券 </h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        卡券删除后无法恢复，确定删除吗？ <br/>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                <button id="okDel" type="button" class="btn btn-primary" data-loading-text="正在删除...">确 定</button>
            </div>
        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": true,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/ud/coupon/data",
                "type": "post",
                "data": function (d) {
                    d.coupon_name = $('#coupon_name').val();
                }
            },
            "order": [[0, "desc"]],
            "columns": [
                {"data": "coupon_name", "bSortable": true},
                {"data": "coupon_notice", "bSortable": true},
                {"data": "coupon_fee", "bSortable": true},
                {"data": "state", "bSortable": true}
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                    	var html='<div ">' +
                        		 '<a class="btn btn-xs btn btn-default" href="${base}/platform/ud/coupon/detail/' + row.id + '" data-pjax >基本信息</a> ' ;
                        if(row.state=="0"){
                        	html+='<a class="btn btn-xs btn btn-default "  href="${base}/platform/ud/coupon/edit/' + row.id + '" data-pjax>修改信息</a> ' +
                        	'<a class="btn btn-xs btn btn-default " href="javascript:;" onclick="del(\'' + row.id + '\')">删除</a>';
                        }
                        html+='</div>';
                        return  html;
                        
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row) {
                    	return getCount(row.id);
                    },
                    "targets": 4
                }
                ,
                {
                    "render": function (data, type, row) {
                    	if(data=="0"){
                        	return "<font color='blue'>未发放</font>";
                    	}else{
                    		return "<font color='red'>已投放</font>";
                    	}
                    },
                    "targets": 3
                }
            ]
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#okDel").unbind("click");
        dialog.find("#okDel").bind("click", function (event) {
            $.post("${base}/platform/ud/coupon/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                dialog.modal("hide");
            }, "json");
        });
    }
    function enableUser(id) {
        $.post("${base}/platform/ud/coupon/enable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disable_" + id).attr("class", "fa fa-circle text-success ml5");
            } else {
                Toast.error(data.msg);
            }
        }, "json");
    }
    function disableUser(id) {
        $.post("${base}/platform/ud/coupon/disable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disable_" + id).attr("class", "fa fa-circle text-danger ml5");
            } else {
                Toast.error(data.msg);
            }
        }, "json");
    }
    $(document).ready(function () {
        initDatatable();
    });

    //获取发放数量
    function getCount(id){
    	var datas=0;
    	$.ajax({
			url : "${base}/platform/ud/coupon/getCount",
			data : {
				id : id
			},
			type : "post",
			async : false,
			dataType : "json",
			success : function(data){
				datas= data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				return false;
			}
		});
    	return datas;
    	
    }
</script>
<%}%>
